package com.jsh.erp.service.bank;

import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.datasource.entities.Bank;
import com.jsh.erp.datasource.entities.Bank;
import com.jsh.erp.datasource.entities.BankExample;
import com.jsh.erp.datasource.mappers.BankMapper;
import com.jsh.erp.datasource.mappers.BankMapperEx;
import com.jsh.erp.exception.JshException;
import com.jsh.erp.service.log.LogService;
import com.jsh.erp.service.systemConfig.SystemConfigService;
import com.jsh.erp.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BankService {
    private Logger logger = LoggerFactory.getLogger(BankService.class);

    @Resource
    private BankMapper bankMapper;

    @Resource
    private BankMapperEx bankMapperEx;
    @Resource
    private LogService logService;
    @Resource
    private SystemConfigService systemConfigService;

    public Bank getBank(String id)throws Exception {
        Bank result=null;
        try{
            result=bankMapper.selectByPrimaryKey(id);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    public List<Bank> getBankListByIds(String ids)throws Exception {
        List<String> idList = StringUtil.strToStringList(ids);
        List<Bank> list = new ArrayList<>();
        try{
            BankExample example = new BankExample();
            example.createCriteria().andIdIn(idList);
            list = bankMapper.selectByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list;
    }

    public List<Bank> getBank()throws Exception {
        BankExample example = new BankExample();
        example.createCriteria().andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
        List<Bank> list=null;
        try{
            list=bankMapper.selectByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list;
    }

    public List<Bank> select(String name, int offset, int rows)throws Exception {
        List<Bank> list=null;
        try{
            list=bankMapperEx.selectByConditionBank(name, offset, rows);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list;
    }

    public Long countBank(String name)throws Exception {
        Long result=null;
        try{
            result=bankMapperEx.countsByBank(name);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int insertBank(JSONObject obj, HttpServletRequest request)throws Exception {
        Bank bank = JSONObject.parseObject(obj.toJSONString(), Bank.class);
        bank.setUploadFlag("0");
        int result=0;
        try{
            result=bankMapper.insertSelective(bank);
            logService.insertLog("银行",
                    new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(bank.getBankName()).toString(), request);
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int updateBank(JSONObject obj, HttpServletRequest request)throws Exception {
        Bank bank = JSONObject.parseObject(obj.toJSONString(), Bank.class);
        bank.setUploadFlag("0");
        int result=0;
        try{
            result=bankMapper.updateByPrimaryKeySelective(bank);
            logService.insertLog("经手人",
                    new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(bank.getBankName()).toString(), request);
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int deleteBank(String id, HttpServletRequest request)throws Exception {
        return batchDeleteBankByIds(id.toString());
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDeleteBank(String ids, HttpServletRequest request) throws Exception{
        return batchDeleteBankByIds(ids);
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDeleteBankByIds(String ids)throws Exception {
        int result =0;
        String [] idArray=ids.split(",");
        //记录日志
        StringBuffer sb = new StringBuffer();
        sb.append(BusinessConstants.LOG_OPERATION_TYPE_DELETE);
        List<Bank> list = getBankListByIds(ids);
        for(Bank bank: list){
            sb.append("[").append(bank.getBankName()).append("]");
        }
        logService.insertLog("经手人", sb.toString(),StringUtil.getRequest());
        //删除经手人
        try{
            result=bankMapperEx.batchDeleteBankByIds(idArray);
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    public int checkIsNameExist(String id, String name) throws Exception{
        BankExample example = new BankExample();
        example.createCriteria().andIdNotEqualTo(id).andBankNameEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
        List<Bank> list =null;
        try{
            list=bankMapper.selectByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list==null?0:list.size();
    }

    public Map<String,String> getBankMap() throws Exception {
        List<Bank> bankList = getBank();
        Map<String,String> bankMap = new HashMap<>();
        for(Bank bank : bankList){
            bankMap.put(bank.getId(), bank.getBankName());
        }
        return bankMap;
    }

    public String getBankByMapAndIds(Map<String,String> bankMap, String bankIds)throws Exception {
        List<String> ids = StringUtil.strToStringList(bankIds);
        StringBuffer sb = new StringBuffer();
        for(String id: ids){
            sb.append(bankMap.get(id)).append(" ");
        }
        return sb.toString();
    }
}
